package org.example.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.example.Order;
import org.example.Product;
import org.example.dao.OrderDao;
import org.example.service.OrderService;
import org.example.service.ProductFeignApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private ProductFeignApi productFeignApi;
    @Override
    public Order createOrder(Long productId, Long userId) {
        log.info("接收到{}号商品的下单请求,接下来调⽤商品微服务查询此商品信息", productId);
//        //远程调⽤商品微服务,查询商品信息
//        String url = "http://localhost:8081/product/"+productId;
//        Product product = restTemplate.getForObject(url,Product.class);
//        log.info("查询到{}号商品的信息,内容是:{}", productId,JSON.toJSONString(product));

//        //从nacos中获取到服务列表保存在本地，并选取其中的第一个来进行远程调用
//        ServiceInstance instance = discoveryClient.getInstances("product-service").get(0);
//        String url = instance.getHost() + ":" + instance.getPort();
//        Product product = restTemplate.getForObject("http://" + url + "/product/" + productId, Product.class);
//        log.info("查询到{}号商品的信息,内容是:{}", productId,JSON.toJSONString(product));

//        //设置本地负载均衡来选择要调用的服务端
//        List<ServiceInstance> instances = discoveryClient.getInstances("product-service");
//        int index = new Random().nextInt(instances.size());
//        ServiceInstance instance = instances.get(index);
//        String url = instance.getHost() + ":" + instance.getPort();
//        log.info(">>从nacos中获取到的微服务地址为:" + url);
//        Product product = restTemplate.getForObject("http://" + url + "/product/" + productId, Product.class);
//        log.info("查询到{}号商品的信息,内容是:{}", productId,JSON.toJSONString(product));

//        //使用Ribbon实现负载均衡
//        Product product = restTemplate.getForObject("http://product-service/product/" + productId, Product.class);
//        log.info("查询到{}号商品的信息,内容是:{}", productId,JSON.toJSONString(product));

          //使用feign实现远程调用和负载均衡
        Product product = productFeignApi.findByPid(productId);
        log.info("查询到{}号商品的信息,内容是:{}", productId,JSON.toJSONString(product));
        //创建订单并保存
        Order order = new Order();
        order.setUid(userId);
        order.setUsername("叩丁狼教育");
        order.setPid(productId);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        orderDao.save(order);
        log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));
        return order;
    }
}
